package com.winit.dataimport;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.WebClientApplication;
import com.winit.commons.util.*;
import com.winit.feature.service.IFeatureService;
import com.winit.menu.model.MatrixMenu;
import com.winit.menu.repository.MatrixMenuRepository;
import com.winit.menu.service.MatrixMenuService;
import com.winit.page.model.Page;
import com.winit.page.repository.PageRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = WebClientApplication.class)
public class WhoMenu {
    @Autowired
    MatrixMenuRepository menuRepository;
    @Autowired
    MatrixMenuService matrixMenuService;
    @Autowired
    PageRepository pageRepository;
    @Autowired
    private IFeatureService featureService;

    private List<MatrixMenu> menuList = new ArrayList<>();
    private List<Page> pageList = new ArrayList<>();

    @Test
    public void Test1() throws Exception {
        JSONObject json = HttpClientUtil.doGetForJson("http://172.16.3.144:180/sc-api-web/SysOpenInterfaceByUser/queryInfosByUser.json?accountName=uswc03&systemName=WHO");
        JSONObject data = json.getJSONObject("data");
        JSONArray menuInfo = data.getJSONArray("menuInfo");
        MatrixMenu rootMenu = getRootMenu();
        List<String> rootChild = new ArrayList<>();
        System.out.println(ObjectToString.getString(rootMenu));
        for (int i = 0; i < menuInfo.size(); i++) {
            JSONObject info = menuInfo.getJSONObject(i);
            MatrixMenu menu = getLevelOneMenu(info, i + 1, rootMenu.getId());
            rootChild.add(menu.getId());
            List<String> children = new ArrayList<>();
            Page page = getPage(menu.getId(), menu.getMenu_name(), menu.getMenuNum());
            children.add(page.getId());
            menu.setChildren(children);
            savePage(page);
            saveMenu(menu);
        }
        rootMenu.setChildren(rootChild);
        saveMenu(rootMenu);
        //保存menu和page
//        menuRepository.save(menuList);
//        for (Page page : pageList) {
//            pageRepository.save(page);
//            featureService.createPrototypeForPage(page);
//        }
    }

    private MatrixMenu getRootMenu() {
        MatrixMenu rootMenu = menuRepository.findRootBySys("WHO");
        if (null == rootMenu) {
            rootMenu = (MatrixMenu) matrixMenuService.createRootMenu("WHO").getData();
        }
        return rootMenu;
    }

    private MatrixMenu getLevelOneMenu(JSONObject json, Integer index, String parentId) throws Exception {
        MatrixMenu menu = new MatrixMenu();
        menu.setId(SeqGenerate.createId("MENU"));
        menu.setMenu_name(json.getString("name"));
        menu.setReq_id("-1");
        menu.setSys("WHO");
        menu.setLevel("1");
        menu.setIs_del("1");
        menu.setStatus("ONLINE");
        Map<String, String> menu_data = new HashMap<>();

        menu_data.put("name", json.getString("name"));
        menu_data.put("routeId", " ");
        menu.setMenu_data(menu_data);
        menu.setParent_id(parentId);
        menu.setMenuNum(NumberUtil.createNumber(NumberUtil.SysEnum.WHO, "ROOT-WHO", NumberUtil.TypeEnum.MENU, index));
        return menu;
    }

    private Page getPage(String menuId, String label, String parent) throws Exception {
        Page page = new Page();
        page.setId(SeqGenerate.createId("MP"));
        page.setFileName(NumberUtil.createNumber(NumberUtil.SysEnum.WHO, parent, NumberUtil.TypeEnum.PAGE, 1));
        page.setMenuId(menuId);
        page.setTitle(label);
        return page;
    }

    private void saveMenu(MatrixMenu menu) {
        System.out.println(ObjectToString.getString(menu));
        menuList.add(menu);
    }
    private void savePage(Page page) {
        System.out.println(ObjectToString.getString(page));
        pageList.add(page);
    }
}
